$(function() {
	var obj = {
		skuid: $(".id"), 					//商品id	
		name: $('.Name'), 					//姓名
		tel: $('.telephone'), 				//联系方式
		Province: $('.province'), 			//省
		City: $('.city'), 					//市
		Area: $('.area'), 					//区
		paymentTotal: $('.paymentTotal'), 	//支付窗口的总价值
		adressId: 0, 						//收货地址ID
		formHoutai: $('.formHoutai'),		//账户集分
		formCount: $('.formCount'),			//商品需要集分
		Goodid: null, 						//商品id	
		skuIdLong: null 					//skuid
	}
	
	//获取缓存中的商品信息，然后展示在页面上
	var order = JSON.parse(localStorage.getItem("indiana"));
	var oimg = order.oimg;
	var num = order.num;
	var pname = order.name;
	var id1 = order.id;
	var point = order.price;
	showItem(oimg,pname,point,num,id1);
	console.log(order);
	//动态创建标签
	function showItem(img, title, point, num, skuid) {
		var orderDetail_con = $('<div class="orderDetail_con"></div>');
		var id = $('<div class="id">' + skuid + '</div>');
		var div_order = $('<div class="orderDetail_con_lt"> <div class = "lt_imgBox" ><img src= ' + img + ' /></div></div>');
		var div_detail = $('<div class="orderDetail_con_rt"><div class="p_top"><p class="name"> ' + title + '</p><p class="red" >￥<span class="point">' + point + '</span>集分</p></div><p class="p_bot"><span class="red" id="red">x<span class="orderNum">' + num + '</span></span></p></div>');
		var delivery = $('<div class="delivery"><span>配送方式</span><div><span>默认快递</span><span>&nbsp;免邮&nbsp;</span><span>&gt;</span></div></div>');
		orderDetail_con.append(id).append(div_order).append(div_detail);
		$(".orderDetail_box").append(orderDetail_con).append(delivery);
	}
	loadPage(); //进入页面加载地址
	function loadPage() {
		if(localStorage.getItem('data')) { //如果有编辑地址也的缓存地址加载缓存地址，首次进入页面加载地址页的默认地址
			var adressData = JSON.parse(localStorage.getItem('data'));
			obj.name.text(adressData.name);
			obj.tel.text(adressData.tel);
			obj.Province.text(adressData.sheng);
			obj.City.text(adressData.shi);
			obj.Area.text(adressData.qu);
			obj.adressId = adressData.id;
			confirmPay(); //如果地址存在才可以使用下单功能
		} else {
			promptlyLoad();
			confirmPay(); //如果地址存在才可以使用下单功能
		}
	}

	//加载地址信息
	function promptlyLoad() {
		$.ajax({
			type: "post",
			url: "http://a.91jfk.com/Product/Address/address_default",
			dataType: 'json',
			success: function(res) {
				console.log(res);
				if(res.code == '1') {
					var data = res.msg;
					obj.adressId = data.id;
					obj.name.text(data.name);
					obj.tel.text(data.tel);
					obj.Province.text(data.area);
					obj.City.text(data.street);
					obj.Area.text(data.address);
				}
			},
			error: function(err) {
				console.log(err)
			}
		});
	}

	//点击顶部跳转管理地址页面
	$('.cfmOrder_content').undelegate().delegate('.address', 'click', function() {
		window.location.href = 'guanlidizhi_exchange.html';
	});

	//点击确认下单按钮
	function confirmPay() {
		
		$('.cfmOrder_cfm').bind('click', function() {
			console.log(obj.adressId,id1,num);
			$.ajax({
				type: "post",
				url: "http://a.91jfk.com/Product/Indiana/go_order",
				data: {
					id: id1,
					aid: obj.adressId,
					num: num,
					platform:1
				},
				success: function(res) {
					console.log(res);
					$(".errAlert").text(res.msg).addClass("AlertShow");
					setInterval(function(){
						window.location.href = "../Shopindex/db_popularity.html"; 
					}, 1500);
				},
				error: function(err) {
					console.log(err);
				}
			}); //AJAX结束
		});
	}

	//点击关闭按钮，关闭支付弹框
	$('.close').bind('click', function() {
		$('.cfmOrder_foot').css('display', 'block');
		$('.payment_foot').css('display', 'none');
		$('.mask').css('display', 'none');
	});

	//纯集分支付、取消支付
    $('.point_cancel').click(function(){
    	$('.mask').hide();
		$('.cfmOrder_foot').show();
		$('.pointPay').hide();
    });

});